package com.xpu.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xpu.pms.comm.http.PageInfo;
import com.xpu.pms.comm.http.QueryPage;
import com.xpu.pms.comm.http.Respond;
import com.xpu.pms.entity.SysRuleEntity;
import com.xpu.pms.mapper.SysRuleMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sys/rule")
public class SysRuleController {
    @Resource
    SysRuleMapper sysRuleMapper;

    @RequestMapping("/list")
    public List<SysRuleEntity> list(){
        List<SysRuleEntity> list = sysRuleMapper.selectList(new LambdaQueryWrapper<>());
        return list;
    }

    @PostMapping("/add")
    public Respond add(@RequestBody SysRuleEntity rule){
        sysRuleMapper.insert(rule);
        try {
            //休眠2秒,让页面上加载状态能够显示出来
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new Respond();
    }
    @PostMapping("/update")
    public Respond update(@RequestBody SysRuleEntity rule){
        sysRuleMapper.updateById(rule);


        return new Respond();
    }
    @RequestMapping("/delete")
    public Respond delete(@RequestBody SysRuleEntity rule){

        sysRuleMapper.deleteById(rule.getId());


        return new Respond();
    }


    @RequestMapping("/page")
    public Respond page(@RequestBody QueryPage page) {
        long index = page.getPage().getIndex();
        long size = page.getPage().getSize();
        IPage<SysRuleEntity> ipage = sysRuleMapper.selectPage(new Page<>(index,size),new QueryWrapper<>());
        PageInfo pageInfo = new PageInfo((int)ipage.getCurrent(),(int)ipage.getTotal(),(int)ipage.getSize());
        Respond res = new Respond().setPage(pageInfo).setData(ipage.getRecords());
        return  res;
    }
}
